package com.insyte.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.insyte.R;
import com.insyte.config.Constants;
import com.insyte.config.InsyteApp;
import com.insyte.model.Contact;
import com.insyte.model.User;

import java.util.List;

/**
 * Created by T430 on 7/14/2015.
 */
public class ContactAdapter extends ArrayAdapter<Contact> {
    private Context context;
    private int layout_id;
    private List<Contact> contacts;
    private LayoutInflater inflater;
    private Holder holder;
    private String url_check_friend_phone = Constants.URL_API;
    private User user;
    private AQuery aQuery;

    public ContactAdapter(Context context, int layout_id, List<Contact> contacts) {
        super(context, layout_id, contacts);
        this.context = context;
        this.layout_id = layout_id;
        this.contacts = contacts;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        user = InsyteApp.getInstance().getUser();
        url_check_friend_phone += "/friend/"+user.getId()+"/fb/check?token="+user.getToken();
        aQuery = new AQuery(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new Holder();
            convertView = inflater.inflate(layout_id, parent, false);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
//            holder.position = position;
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        Contact contact = contacts.get(position);
        holder.tv_name.setText(contact.getName());

        setEvent(holder.tv_name, contact.getNumber());

        return convertView;
    }

    private void setEvent(TextView v, final List<String> phones) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url_check_friend_phone += "&number=" + phones.get(0) + "&country_code=84";
                runAjaxCheckPhone();
            }
        });
    }

    private void runAjaxCheckPhone() {
        aQuery.ajax(url_check_friend_phone, String.class, new AjaxCallback<String>(){
            @Override
            public void callback(String url, String result, AjaxStatus status) {
                Log.e("result search phone", result);
            }
        });
    }

    private static class Holder {
        TextView tv_name;
        CheckBox checkbox;
        int position;
    }
}
